﻿using Red.Controls.Shell;
using System;
using System.Windows;
using System.Windows.Threading;

namespace Red.Controls.Notification
{
    /// <summary>
    /// Interaction logic for NotificationPopupWindow.xaml
    /// </summary>
    public partial class NotificationPopupWindow : RedWindow
    {
        /// <summary>
        /// Timer, on it's tick, the window is closed.
        /// </summary>
        private DispatcherTimer _timer = new DispatcherTimer();

        /// <summary>
        /// Duration of the notification, time after which the window is closed.
        /// </summary>
        private const int _duration = 4;

        /// <summary>
        /// Title of the notification.
        /// </summary>
        public string Text
        {
            get
            {
                return _notification.Text;
            }

            set
            {
                _notification.Text = value;
            }
        }
        
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="parentWindow">Parent window which this notification belongs to.</param>
        public NotificationPopupWindow( Window parentWindow )
        {
            InitializeComponent();
            Owner = parentWindow;
            _timer.Tick += OnTick;
            ShowInTaskbar = false;
            ShowTitleBar = false;
            ShowActivated = false;  // To prevent from stealing focus from currently focused window
        }

        /// <summary>
        /// Shows the notification and starts the count down.
        /// </summary>
        public void StartNotification()
        {
            Show();
            _timer.Interval = new TimeSpan( 0, 0, _duration );
            _timer.Start();
        }
        
        /// <summary>
        /// Close the notification on first tick (after _duration time).
        /// </summary>
        private void OnTick( object sender, EventArgs e )
        {
            Close();
        }
    }
}
